package eproject.mm.com.projectsupervision.service;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import eproject.mm.com.projectsupervision.util.ServiceUtil;

public class KeepService extends Service {
    private static final String TAG = KeepService.class.getName();

    private String Process_Name = "eproject.mm.com.projectsupervision:localService";

    public KeepService() {
    }

//    private StrongService startS1 = new StrongService.Stub() {
//        @Override
//        public void stopService() throws RemoteException {
//            Intent i = new Intent(getBaseContext(), DemoService.class);
//            getBaseContext().stopService(i);
//        }
//
//        @Override
//        public void startService() throws RemoteException {
//            Intent i = new Intent(getBaseContext(), DemoService.class);
//            getBaseContext().startService(i);
//
//        }
//    };

    public void onCreate() {
        //android.os.Debug.waitForDebugger();
        super.onCreate();
        init();
        //keepServiceDemo();
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Intent intents = new Intent();
        intents.setClass(this, DemoService.class);
        bindService(intents, conn, Context.BIND_IMPORTANT);
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    private MyBinder binder;
    private ServiceConnection conn;
    private void init() {
        if (conn == null) {
            conn = new MyConnection();
        }
        binder = new MyBinder();
    }

    static class MyBinder extends StrongService.Stub {
        @Override
        public void startService() throws RemoteException {
        }
        @Override
        public void stopService() throws RemoteException {
        }
    }
    class MyConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(this.getClass().getName(),"******************----Keep Connect----******************");
        }

        @Override
        public void onServiceDisconnected(ComponentName nme) {
            KeepService.this.startService(new Intent(KeepService.this,
                    DemoService.class));
            KeepService.this.bindService(new Intent(KeepService.this,
                    DemoService.class), conn, Context.BIND_IMPORTANT);
        }
    }
}
